package com.cscj.android.repository.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Upsert
import com.cscj.android.repository.db.entity.SearchEngineEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface SearchEngineDao {

    @Upsert
    suspend fun upsertSearchEngine(searchEngineEntity: SearchEngineEntity)

    @Upsert
    suspend fun batchInsert(engineList: List<SearchEngineEntity>)

    @Query("update search_engine set preferred = :preferred")
    suspend fun updatePreferred(preferred: Boolean)

    @Query("update search_engine set preferred = :preferred where id = :id")
    suspend fun updatePreferredById(id: Int, preferred: Boolean)

    @Query("select * from search_engine order by id asc")
    fun engineList(): Flow<List<SearchEngineEntity>>

    @Query("select * from search_engine where preferred = 1 limit 1")
    fun currentEngine(): Flow<SearchEngineEntity?>

}